# 捕获文件不存在的异常 FileNotFoundError
try:
    f = open('E:/PythonBasicKnowledge/08 异常、模块和包/happy.txt', 'r', encoding='UTF-8')
except:  # 没有指定异常名称👉捕获的是全部异常
    print('😟文件不存在👉现在以w模块打开文件')
    f = open('E:/PythonBasicKnowledge/08 异常、模块和包/happy.txt', 'w', encoding='UTF-8')

# 捕获指定的异常
try:
    print(name)
except NameError as ne:
    print('变量未定义异常', ne)

# 捕获多个异常
try:
    # print(name)
    r = 1 / 0
except (NameError, ZeroDivisionError) as e:
    print('捕获到除0或变量为定义异常')

# 捕获全部异常
try:
    open('E:/PythonBasicKnowledge/08 异常、模块和包/happyNice.txt', 'r', encoding='UTF-8')
    1 / 0
except Exception as e:
    print('捕获到了Exception异常', e)

# else
try:
    print('hello')
except Exception as e:
    print('出现了Exception异常')
else:
    print('没有出现异常 hello world')

# finally
try:
    f = open('E:/PythonBasicKnowledge/08 异常、模块和包/great.txt', 'r', encoding='UTF-8')
except FileNotFoundError as e:
    print('捕获到了文件不存在异常', e)
    f = open('E:/PythonBasicKnowledge/08 异常、模块和包/great.txt', 'w', encoding='UTF-8')
else:
    print('😀没有出现异常')
finally:
    print('finally-(*^_^*)')
    f.close()
